/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/
#include <iostream>

using namespace std;

int main()
{
    int a;
    std::cin >> a;
    int b = a;
    bool z = false;
    int v = 0;
    int max = -2147483648;
    if((a > 9999) && (a < 1000))
    {
        cout << "0";
    }
    else
    {
        for(int i = 2; i < a; i++)
        {
            if(a % i == 0)
            {
                z = true;
            }
            else if((i == a - 1) && (z == false))
            {
                std::cout << a << std::endl;
                return 0;
            }
        }
        
        int x = b % 1000;
        for(int i = 2; i < x; i++)
        {
            if(x % i == 0)
            {
                break;
            }
            else if(i == x - 1)
            {
                if (x > max)
                {
                    max = x;
                }
            }
        }
        x = b / 10;
        for(int i = 2; i < x; i++)
        {
            if(x % i == 0)
            {
                break;
            }
            else if(i == x - 1)
            {
                if (x > max)
                {
                    max = x;
                }
            }
        }
        x = b / 100;
        for(int i = 2; i < x; i++)
        {
            if(x % i == 0)
            {
                break;
            }
            else if(i == x - 1)
            {
                if (x > max)
                {
                    max = x;
                }
            }
        }
        x = (b / 10) % 100;
        for(int i = 2; i < x; i++)
        {
            if(x % i == 0)
            {
                break;
            }
            else if(i == x - 1)
            {
                if (x > max)
                {
                    max = x;
                }
            }
        }
        x = b % 100;
        for(int i = 2; i < x; i++)
        {
            if(x % i == 0)
            {
                break;
            }
            else if(i == x - 1)
            {
                if (x > max)
                {
                    max = x;
                }
            }
        }
        x = b / 1000;
        for(int i = 2; i < x; i++)
        {
            if(x % i == 0)
            {
                break;
            }
            else if(i == x - 1)
            {
                if (x > max)
                {
                    max = x;
                }
            }
        }
        x = (b / 100) % 10;
        for(int i = 2; i < x; i++)
        {
            if(x % i == 0)
            {
                break;
            }
            else if(i == x - 1)
            {
                if (x > max)
                {
                    max = x;
                }
            }
        }
        x = (b / 10) % 10;
        for(int i = 2; i < x; i++)
        {
            if(x % i == 0)
            {
                break;
            }
            else if(i == x - 1)
            {
                if (x > max)
                {
                    max = x;
                }
            }
        }
        x = b % 10;
        for(int i = 2; i < x; i++)
        {
            if(x % i == 0)
            {
                break;
            }
            else if(i == x - 1)
            {
                if (x > max)
                {
                    max = x;
                }
            }
        }
    }
    if(max != -2147483648)
    {
        std::cout << max << std::endl;
        return 0;
    }
    else
    {
        std::cout << 0 << std::endl;
        return 0;
    }
}
